package com.xymtop.Controller;

import com.xymtop.Annotation.Controller;
import com.xymtop.Annotation.Mapping;
import com.xymtop.Annotation.Rest;
import com.xymtop.Entity.Tea;
import com.xymtop.Server.Util.DBService;
import com.xymtop.Vo.Teacher;
import com.xymtop.Server.ResoultJson;
import com.xymtop.Server.ServerStatus;
import com.xymtop.Service.TeaService;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @ClassName : TeacherController
 * @Description : 教师的控制器
 * @Author : 肖叶茂
 * @Date: 2022/12/13  16:31
 */
@Controller(router = "Teacher")
public class TeacherController extends HttpServlet {

    private TeaService teaService = new TeaService();

    @Mapping(url = "login")
    @Rest
    public ResoultJson<Teacher> login(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        String id = request.getParameter("id");
        String password = request.getParameter("password");

        Teacher teacher =  teaService.login(id,password);
        if(teacher==null){
            return  new ResoultJson<>(204);
        }else {
            return new ResoultJson<>(200,teacher,"");
        }
    }

    @Mapping(url = "regist")
    @Rest
    public  ResoultJson<Tea> regist(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, InstantiationException, IllegalAccessException {

        String password = request.getParameter("password");
        String name = request.getParameter("name");
        String title = request.getParameter("title");
        String sex = request.getParameter("sex");
        String detail = request.getParameter("detail");

        Tea teacher = new Tea();
        teacher.setTitle(title);
        teacher.setName(name);
        teacher.setPassword(password);
        teacher.setSex(sex);


        Tea res = teaService.regist(teacher);

        if(res == null){
            return  new ResoultJson<>(ServerStatus.NOTHAVE);
        }else {
            return  new ResoultJson<>(ServerStatus.SUCCESS,res,"注册成功");
        }
    }

        @Mapping(url = "getList")
        @Rest
        public  ResoultJson<Object> getList(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, InstantiationException, IllegalAccessException {

            List<Object> objects = DBService.Get("SELECT * FROM `t_login`",Teacher.class.getCanonicalName());
            if(objects.isEmpty()){
                return  new ResoultJson<>(ServerStatus.NOTHAVE);
            }else {
                return  new ResoultJson<>(0,objects,"");
            }
        }

}
